//
//  ActivityDetailController.m
//  UI-豆瓣项目APP
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 孙鹏. All rights reserved.
//

#import "ActivityDetailController.h"
#import "DateBaseSingleton.h"
#import "ActivityDetailTableViewCell.h"
#import "ActivityDetailViewController.h"
@interface ActivityDetailController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation ActivityDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.userActivityDetailVC = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.userActivityDetailVC.delegate = self;
    self.userActivityDetailVC.dataSource = self;
    [self.view addSubview:self.userActivityDetailVC];
    
    [self.userActivityDetailVC registerClass:[ActivityDetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    self.navigationItem.title = @"我的活动";
    
    
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return UITableViewCellEditingStyleDelete;
    
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {

        
      NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
        
        
        
        NSMutableArray *array = [NSMutableArray array];
        
        array = [[DateBaseSingleton shareDateBase]selectActivityDetailName:userName];
        NSString *titleName = [array objectAtIndex:indexPath.row];
        
        [[DateBaseSingleton shareDateBase]deleteActivityDetail:titleName];
        
        
        [self.userActivityDetailVC reloadData];
        
    }
    
}





-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 60;
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    
     NSMutableArray *array = [NSMutableArray array];
    array = [[DateBaseSingleton shareDateBase]selectActivityDetail];

    self.rowArray = [NSMutableArray array];
    for (Activitylist *act in array) {
        if ([act.userName isEqualToString:userName]) {
            
            [self.rowArray addObject:act];
            
        }
    }
    
    return self.rowArray.count;
 
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdenfier =@"cell";
    ActivityDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdenfier];
    
    Activitylist *act = [[Activitylist alloc]init];
    act = [self.rowArray objectAtIndex:indexPath.row];
    cell.titleName.text = act.title;
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityDetailViewController *actDVC = [[ActivityDetailViewController alloc]init];
    
    Activitylist *act = [[Activitylist alloc]init];
    act = [self.rowArray objectAtIndex:indexPath.row];
    
    actDVC.activity = act;
    
    [self.navigationController pushViewController:actDVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
